package com.jpemartin.jgt;
import java.awt.Color;

/** Graphically displays the time it took to draw a frame.
 *  The yellow bar is the instant reading; the white tab indicates maximum (decays),
 *  and the yellow text is the average over the last 10 frames. **/
public class FrameSpeedBar {
    protected long maxInfo;
    protected String label;
    protected String avgInfo;
    protected int dc;
    protected int sampleCount;
    protected int sampleSum;
    protected long graphDivisor;
    protected Color color;
    
    public FrameSpeedBar() {
        label="ms";
        avgInfo="";
        graphDivisor=1;
        color = Color.YELLOW;
    }
    
    public FrameSpeedBar(String label, long graphDivisor) {
       this.label=label;
       this.graphDivisor = graphDivisor;
       avgInfo="";
   }
    
    public void show(java.awt.Graphics g, long sample, int x, int y) {
        sampleCount++;
        sampleSum += sample;
        long info = sample/graphDivisor;
        if (info*2>=maxInfo) {
            maxInfo = info*2;
            dc=0;
        } else {
            maxInfo -= dc;
            if (dc<5) dc++;
        }
        g.setColor(Color.WHITE);
        g.fillRect((int)(x-5-(maxInfo/2)),y-10,(int)1,10);
        g.fillRect((int)(x-5-(maxInfo/2)),y-5,(int)((maxInfo/2)-info),1);
        g.setColor(color);
        g.fillRect((int)(x-5-info),y-10,(int)info,10);
        if (sampleCount>10) {
            avgInfo = (sampleSum/sampleCount)+label;
            if (sampleSum<0) color=(Color.WHITE);
            else color=(Color.YELLOW);
            sampleCount=0;
            sampleSum=0;
        }
        g.drawString(avgInfo,x,y);        
    }
    
}
